package scene;

import game.Consts;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import raytracer.Vector3f;

public class SceneLoader {
	// / Read the file with the given file name and return its contents in a
	// string.
	public static Scene loadScene(String fileName) {
		Scene s = new Scene();
		boolean givelife = false;
		int score = 30;
		s.targets = 0;

		ArrayList<Vector3f> vs = new ArrayList<Vector3f>();

		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(fileName));

			Material lastM = new Material();
			s.camera = new Camera(45, 0, 0, 0, 0, 0, 1, 0, 1, 0);

			while (br.ready()) {
				String line = br.readLine().replaceAll("  ", " ");

				String[] parts = line.split(" ");

				if (parts[0].equalsIgnoreCase("render_info")) {
					int w = Integer.parseInt(parts[1]);
					int h = Integer.parseInt(parts[2]);
					String i = parts[3];
					s.renderInfo = new RenderInfo(i, w, h);
				}
				if (parts[0].equalsIgnoreCase("film_resolution")) {
					int w = Integer.parseInt(parts[1]);
					int h = Integer.parseInt(parts[2]);
					s.renderInfo = new RenderInfo(null, w, h);
				}
				if (parts[0].equalsIgnoreCase("camera")) {
					float px = Float.parseFloat(parts[1]);
					float py = Float.parseFloat(parts[2]);
					float pz = Float.parseFloat(parts[3]);
					float dx = Float.parseFloat(parts[4]);
					float dy = Float.parseFloat(parts[5]);
					float dz = Float.parseFloat(parts[6]);
					float ux = Float.parseFloat(parts[7]);
					float uy = Float.parseFloat(parts[8]);
					float uz = Float.parseFloat(parts[9]);
					float ha = Float.parseFloat(parts[10]);

					Camera c = new Camera(ha, px, py, pz, dx, dy, dz, ux, uy, uz);
					s.camera = c;
				}
				if (parts[0].equalsIgnoreCase("material")) {
					float ar = Float.parseFloat(parts[1]);
					float ag = Float.parseFloat(parts[2]);
					float ab = Float.parseFloat(parts[3]);
					float dr = Float.parseFloat(parts[4]);
					float dg = Float.parseFloat(parts[5]);
					float db = Float.parseFloat(parts[6]);
					float sr = Float.parseFloat(parts[7]);
					float sg = Float.parseFloat(parts[8]);
					float sb = Float.parseFloat(parts[9]);
					float ns = Float.parseFloat(parts[10]);
					float tr = Float.parseFloat(parts[11]);
					float tg = Float.parseFloat(parts[12]);
					float tb = Float.parseFloat(parts[13]);
					float ior = Float.parseFloat(parts[14]);

					lastM = new Material(new Color(ar, ag, ab), new Color(dr, dg, db), new Color(sr, sg, sb), ns,
							new Color(tr, tg, tb), ior);
				}
				if (parts[0].equalsIgnoreCase("sphere")) {
					float x = Float.parseFloat(parts[1]);
					float y = Float.parseFloat(parts[2]);
					float z = Float.parseFloat(parts[3]);
					float r = Float.parseFloat(parts[4]);

					Sphere sp = new Sphere(r, x, y, z, lastM);
					sp.givelife = givelife;
					sp.score = score;
					
					s.shapes.add(sp);
					s.targets ++;
				}
				if (parts[0].equalsIgnoreCase("point_light")) {
					float r = Float.parseFloat(parts[1]);
					float g = Float.parseFloat(parts[2]);
					float b = Float.parseFloat(parts[3]);
					float x = Float.parseFloat(parts[4]);
					float y = Float.parseFloat(parts[5]);
					float z = Float.parseFloat(parts[6]);

					s.pointLights.add(new PointLight(x, y, z, new Color(r, g, b)));
				}
				if (parts[0].equalsIgnoreCase("directional_light")) {
					float r = Float.parseFloat(parts[1]);
					float g = Float.parseFloat(parts[2]);
					float b = Float.parseFloat(parts[3]);
					float dx = Float.parseFloat(parts[4]);
					float dy = Float.parseFloat(parts[5]);
					float dz = Float.parseFloat(parts[6]);

					s.directionalLights.add(new DirectionalLight(dx, dy, dz, new Color(r, g, b)));
				}
				if (parts[0].equalsIgnoreCase("ambient_light")) {
					float r = Float.parseFloat(parts[1]);
					float g = Float.parseFloat(parts[2]);
					float b = Float.parseFloat(parts[3]);

					s.ambient = new AmbientLight(new Color(r, g, b));
				}
				if (parts[0].equalsIgnoreCase("max_depth")) {
					s.maxDepth = Integer.parseInt(parts[1]);
					if(s.maxDepth > Consts.GLOBAL_MAXDEPTH) s.maxDepth = Consts.GLOBAL_MAXDEPTH;
				}
				if (parts[0].equalsIgnoreCase("vertex")) {
					float x = Float.parseFloat(parts[1]);
					float y = Float.parseFloat(parts[2]);
					float z = Float.parseFloat(parts[3]);

					vs.add(new Vector3f(x, y, z));
				}
				if (parts[0].equalsIgnoreCase("triangle")) {
					int v3 = Integer.parseInt(parts[1]);
					int v2 = Integer.parseInt(parts[2]);
					int v1 = Integer.parseInt(parts[3]);

					s.shapes.add(new Triangle(vs.get(v1), vs.get(v2), vs.get(v3), lastM));
				}
				if (parts[0].equalsIgnoreCase("background")) {
					float r = Float.parseFloat(parts[1]);
					float g = Float.parseFloat(parts[2]);
					float b = Float.parseFloat(parts[3]);

					s.renderInfo.bgCol = new Color(r, g, b);
				}
				if (parts[0].equalsIgnoreCase("backgroundimage")) {
					s.renderInfo = new RenderInfo(parts[1], 100, 100);
				}
				if (parts[0].equalsIgnoreCase("lives")) {
					s.lives = Integer.parseInt(parts[1]);
				}
				if (parts[0].equalsIgnoreCase("givelife")) {
					givelife = Boolean.parseBoolean(parts[1]);
				}
				if (parts[0].equalsIgnoreCase("score")) {
					score = Integer.parseInt(parts[1]);
				}
			}

			return s;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

}